package com.huice;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.jayway.jsonpath.JsonPath;

public class AppiumServer {

	public static void main(String[] args) throws IOException {
		Runtime.getRuntime().exec("cmd /c start appium.cmd --session-override");
		System.out.println(isRunning());
	}
	
	public static boolean  isRunning(){
		boolean flag = false;
		CloseableHttpClient client = HttpClients.createDefault();
		try {
			URI url = new URIBuilder()
							.setScheme("http")
							.setHost("127.0.0.1")
							.setPort(4723)
							.setPath("/wd/hub/status").build();
			HttpGet get = new HttpGet(url);
			InputStream in = client.execute(get).getEntity().getContent();
			Object status = JsonPath.read(in, "$..status");
			if(status.toString().equals("[0]")){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("建立get请求失败");
		}
		return flag;
	}
}
